package org.eoti.elec;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import java.util.concurrent.*;

public class DigitalComponent
implements TickListener
{
	protected int propagationDelay;
	protected int currentDelay;
	protected String name;
	protected CopyOnWriteArrayList<Pin> inputPins;
	protected CopyOnWriteArrayList<Pin> outputPins;
	protected CopyOnWriteArrayList<Pin> outputBuffer;

	/**
	 * @param name of this component
	 * @param propagationDelay in tenths of nanoseconds (100s of picoseconds)
	 */
	public DigitalComponent(String name, int propagationDelay)
	{
		this.name = name;
		this.propagationDelay = propagationDelay;
		inputPins = new CopyOnWriteArrayList<Pin>();
		outputPins = new CopyOnWriteArrayList<Pin>();
		outputBuffer = new CopyOnWriteArrayList<Pin>();
		TickGenerator.getInstance().addTickListener(this);
	}

	public String getName(){return name;}

	protected Pin getBufferedPin(String name)
	{
		for(Pin pin : outputBuffer)
		{
			if(pin.getName().equals(name))
				return pin;
		}

		return null;
	}

	public Pin getPin(String name)
	{
		for(Pin pin : inputPins)
		{
			if(pin.getName().equals(name))
				return pin;
		}

		for(Pin pin : outputPins)
		{
			if(pin.getName().equals(name))
				return pin;
		}

		return null;
	}

	protected Pin getBufferedPin(int pinNumber)
	{
		for(Pin pin : outputBuffer)
		{
			if(pin.getPinNumber() == pinNumber)
				return pin;
		}

		return null;
	}

	public Pin getPin(int pinNumber)
	{
		for(Pin pin : inputPins)
		{
			if(pin.getPinNumber() == pinNumber)
				return pin;
		}

		for(Pin pin : outputPins)
		{
			if(pin.getPinNumber() == pinNumber)
				return pin;
		}

		return null;
	}

	protected void addPin(Pin pin)
	{
		if(pin.getType() == Pin.Type.Output)
		{
			outputPins.add(pin);
			outputBuffer.add(new Pin(pin.getPinNumber(), pin.getName(), pin.getType(), pin.getState()));
		}else{
			inputPins.add(pin);
		}
	}

	public void tick()
	{
		if((currentDelay++) >= propagationDelay)
		{
			for(Pin pin : outputPins)
				pin.setState(getBufferedPin(pin.getName()).getState());

			currentDelay = 0;
		}
	}
}
